<!-- 
	app版本更新 
	props:
		isAutoCheck: 是否自动检查
		appId：app包名
	action:
		checkVersion: 请求版本及对比
		noNow: 暂不更新
		upNow: 立即更新
-->
<template>
    <uni-popup class="pop-check-version" ref="checkAppVersion" type="center" :maskClick="false">
        <view class="pop-check-version-box">
            <view class="version-main">
                <text class="version-title">发现新版本</text>
                <view class="version-content">
                    <text class="version-content-text">{{ content }}</text>
                    <view class="progress-box" v-if="progress > 0">下载中：{{ progress.toFixed(0) }} %</view>
                </view>
            </view>
            <view class="version-action">
                <button class="btn btn-no-now" v-if="!isForceUpdate" @click="noNow">稍后再说</button>
                <button class="btn" @click="upNow">立即更新</button>
            </view>
        </view>
    </uni-popup>
</template>

<script>
import { getAppVersionForServer, downApp } from "./util.js"
import { getPlatform } from "@/uni-sub-base/utils/platformUtil.js"
export default {
    components: {},
    props: {
        isAutoCheck: {
            type: Boolean,
            default: false,
        },
        appId: {
            type: String,
            default: "",
        },
    },
    data() {
        return {
            isForceUpdate: false,
            downUrl: "",
            content: "",
            appstoreUrl: "",
            progress: 0,
            isDowndding: false,
        }
    },
    mounted() {
        if (this.isAutoCheck) {
            this.checkVersion()
        }
    },
    methods: {
        //检查版本
        checkVersion() {
            //#ifdef APP
            getAppVersionForServer(this.appId).then((res) => {
                if (res && res.hasNew) {
                    this.content = res.message
                    this.downUrl = res.data.downUrl
                    this.isForceUpdate = res.data.forceUpdate
                    this.appstoreUrl = res.data.appstoreUrl
                    this.$forceUpdate()
                    this.$refs.checkAppVersion.open()
                } else if (!this.isAutoCheck) {
                    uni.showToast({
                        title: "当前已是最新版本",
                        icon: "none",
                    })
                }
            })
            //#endif
        },
        //暂不更新
        noNow() {
            this.$emit("noNowBack", "")
            this.$refs.checkAppVersion.close()
        },
        //立即更新
        upNow() {
            if (this.isDowndding) {
                return
            }
            this.isDowndding = true
            //监听网络
            uni.getNetworkType({
                success: (res) => {
                    if (res.networkType == "none") {
                        this.easyToast("请检查网络后重试")
                        this.isDowndding = false
                        return
                    }
                    if (getPlatform() == "android") {
                        downApp(this.downUrl, (progressBack) => {
                            this.isDowndding = false
                            this.progress = progressBack
                            // if(this.progress >= 100){
                            // 	this.$refs.checkAppVersion.close()
                            // }
                        })
                    } else if (getPlatform() == "ios") {
                        this.isDowndding = false
                        //格式 itunes.apple.com/app/idxxx
                        if (this.appstoreUrl) {
                            let url = "itms-apps://" + this.appstoreUrl
                            plus.runtime.openURL(url)
                        }
                    }
                },
                fail(err) {
                    console.log(err)
                },
            })
        },
    },
}
</script>

<style lang="scss">
.pop-check-version {
    z-index: 999;
}
.pop-check-version-box {
    padding-top: 40rpx;
    background: #ffffff;
    width: 580rpx;
    height: auto;
    min-height: 320rpx;
    border-radius: 16rpx;
    display: flex;
    flex-direction: column;
    .version-main {
        flex: 1;
        .version-title {
            display: flex;
            justify-content: center;
            font-size: 36rpx;
            font-weight: bold;
            color: #000000;
            text-align: center;
            margin-bottom: 30rpx;
        }
        .version-content {
            padding: 0 70rpx 40rpx;
            .version-content-text {
                font-size: 28rpx;
                color: #000000;
                line-height: 50rpx;
            }
            .progress-box {
                display: flex;
                margin-top: 20rpx;
                justify-content: center;
                color: #999999;
            }
        }
    }
    .version-action {
        border-top: solid 1rpx rgba(0, 0, 0, 0.1);
        display: flex;
        flex-direction: row;
        .btn {
            display: flex;
            flex: 1;
            height: 110rpx;
            font-size: 32rpx;
            color: $app-color-main;
            border-radius: 0;
            margin: 0;
        }
        .btn-no-now {
            border-right: solid 1rpx rgba(0, 0, 0, 0.1);
            color: #666666;
        }
    }
}
</style>
